﻿package me.hvs.farmerdroid;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private FarmService mService = null;
    private ServiceConnection mConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
			mService = ((FarmService.LocalBinder)arg1).getService();
		}
		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			mService = null;
		}
    };
    
	private ListView farmStateListView;
	private ArrayList<FarmField>farmState = new ArrayList<FarmField>();
	private FarmStateListAdapter stateAdapter;
	
	private class FarmStateListAdapter extends ArrayAdapter<FarmField>{
		private int resourceId;
		public FarmStateListAdapter(Context context, int textViewResourceId, ArrayList<FarmField>objects){   
		       super(context, textViewResourceId, objects);   
		       this.resourceId = textViewResourceId;   
		 }   
		        
		 @Override   
		 public View getView(int position, View convertView, ViewGroup parent){   
			 FarmField user = getItem(position);
			 LinearLayout userListItem = new LinearLayout(getContext());   
			 String inflater = Context.LAYOUT_INFLATER_SERVICE;    
			 LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);    
			 vi.inflate(resourceId, userListItem, true);   
			 TextView tvUsername = (TextView)userListItem.findViewById(R.id.farm_state_list_cropname);   
			 TextView tvAskedNum = (TextView)userListItem.findViewById(R.id.farm_state_list_harvesttime);
			 tvUsername.setText(user.plantName);   
			 tvAskedNum.setText(String.valueOf(user.timeToHarvest));
			 return userListItem;   
		 }

	}

    Timer timer = new Timer();
	private static class MyHandler extends Handler {
	   WeakReference<MainActivity> mActivity;
	   MyHandler(MainActivity activity) {
	      mActivity = new WeakReference<MainActivity>(activity);
	   }
	   @Override
	   public void handleMessage(Message msg) {
		   MainActivity theActivity = mActivity.get();
		   if(theActivity == null)
			   return;
		   switch (msg.what) {
		   case 1:
			   theActivity.getFarmState();
			   theActivity.stateAdapter.notifyDataSetChanged();
			   break;
		   }
	   }
	}
	MyHandler handler = new MyHandler(this);
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	Log.d("MainActivity", "OnCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        farmStateListView = (ListView)findViewById(R.id.farmStateListView);

        Intent intent = new Intent(this, FarmService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

        //获取 farmState
        this.getFarmState();
        stateAdapter = new FarmStateListAdapter(this,R.layout.list_state_item,farmState);
        farmStateListView.setAdapter(stateAdapter);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
            	handler.sendEmptyMessage(1);
            }
        }, 0, 1000);
    }

    @Override
    protected void onDestroy() {
    	Log.d("MainActivity", "onDestroy()");
    	super.onDestroy();
    	unbindService(mConnection);
    	timer.cancel();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private void getFarmState(){
    	/*
    	farmState = new ArrayList<FarmField>();
    	Date nowTime = new Date();
    	FarmField test = new FarmField("作物",nowTime,"详细信息");
    	farmState.add(test);
    	farmState.add(test);
    	*/
		farmState.clear();
    	if (mService != null)
    		farmState.addAll(mService.getFieldState());
    }
}
